# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations

#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
## - call exposes all registered services (none by default)
#########################################################################

def index():
    """
    example action using the internationalization operator T and flash
    rendered by views/default/index.html or views/generic.html
    """
    title='Choose a task'
    message=T('Welcome to the game server webbased control panel')
    menuitems=[]
    menuitems.append(('',T(''), ''))
    menuitems.append(('64_usermanagement',T('Sessions'),'sessions'))
    menuitems.append(('64_usermanagement',T('RPC Calls'),'rpc_calls'))
    #menuitems.append(('64_groupmanagement',T('Manage groups'),'manageGroups'))
    #menuitems.append(('64_importdata',T('Import data'),'importMenu'))
    #menuitems.append(('64_exportdata',T('Export data'),'exportMenu'))
    return dict(title=title, message=message, menuitems=menuitems)

@auth.requires_membership('admins')
def sessions():
    response.view='dashboard/manage.html'
    title=T("Manage sessions")
    grid = SQLFORM.grid(db.session,    
             searchable=True,  
             sortable=True,  
             paginate=30,   
             deletable=False, 
             editable=True, 
             details=True, 
             selectable=None, 
             create=True, 
             csv=False,
             links_in_grid=None, 
             #linked_tables=['Category'], 
             user_signature= None,
             #maxtextlengths={'vvb_items.title':30,'vvb_items.summary':75}, 
             maxtextlength=20, 
             onvalidation=None, 
             oncreate=None, 
             onupdate=None, 
             ondelete=None, 
             sorter_icons=('[^]','[v]'),  
             #ui = 'jquery-ui',  
             #showbuttontext=None,
             _class="web2py_grid",  
             formname='web2py_grid')
    return dict(title=title, grid=grid)

@auth.requires_membership('admins')
def rpc_calls():
    response.view='dashboard/manage.html'
    title=T("Manage rpc_calls")
    grid = SQLFORM.grid(db.rpc_call,    
             searchable=True,  
             sortable=True,  
             paginate=30,   
             deletable=False, 
             editable=True, 
             details=True, 
             selectable=None, 
             create=True, 
             csv=False,
             links_in_grid=None, 
             #linked_tables=['Category'], 
             user_signature= None,
             #maxtextlengths={'vvb_items.title':30,'vvb_items.summary':75}, 
             maxtextlength=20, 
             onvalidation=None, 
             oncreate=None, 
             onupdate=None, 
             ondelete=None, 
             sorter_icons=('[^]','[v]'),  
             #ui = 'jquery-ui',  
             #showbuttontext=None,
             _class="web2py_grid",  
             formname='web2py_grid')
    return dict(title=title, grid=grid)

def user():
    """
    exposes:
    http://..../[app]/default/user/login
    http://..../[app]/default/user/logout
    http://..../[app]/default/user/register
    http://..../[app]/default/user/profile
    http://..../[app]/default/user/retrieve_password
    http://..../[app]/default/user/change_password
    use @auth.requires_login()
        @auth.requires_membership('group name')
        @auth.requires_permission('read','table name',record_id)
    to decorate functions that need access control
    """
    return dict(form=auth())

#@auth.requires_membership('admins')
@auth.requires_login()
def adminPanel():
    form1=SQLFORM(db.auth_membership)
    if form1.process().accepted:
        redirect(URL())
    elif form1.errors:
        response.flash = T('form has errors')
    else:
        response.flash = T('please fill the form')
    form2=SQLFORM(db.auth_group)
    if form2.process().accepted:
        redirect(URL())
    elif form2.errors:
        response.flash = T('form has errors')
    else:
        response.flash = T('please fill the form')
    allGroups=db(db.auth_group).select()
    allMemberships=db(db.auth_membership).select()
    return dict(form1=form1,form2=form2,allGroups=allGroups,allMemberships=allMemberships)

@auth.requires_membership('admins')
def deleteGroup():
    query = db(db.auth_group.id==request.args(0)).select().first()
    remove = db(db.auth_group.id==query).delete()
    if remove:
        redirect(URL('adminPanel'))
    return dict(remove=remove)

#TODO ENABLE ON PRODUCTION
#@auth.requires_membership('admins')
def deleteMembership():
    query = db(db.auth_membership.id==request.args(0)).select().first()
    remove = db(db.auth_membership.id==query).delete()
    if remove:
        redirect(URL('adminPanel'))
    return dict(remove=remove)

@auth.requires_login()    
def download():
    """
    allows downloading of uploaded files
    http://..../[app]/default/download/[filename]
    """
    return response.download(request,db)

def call():
    """
    exposes services. for example:
    http://..../[app]/default/call/jsonrpc
    decorate with @services.jsonrpc the functions to expose
    supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
    """
    return service()

@auth.requires_signature()
def data():
    """
    http://..../[app]/default/data/tables
    http://..../[app]/default/data/create/[table]
    http://..../[app]/default/data/read/[table]/[id]
    http://..../[app]/default/data/update/[table]/[id]
    http://..../[app]/default/data/delete/[table]/[id[
    http://..../[app]/default/data/select/[table]
    http://..../[app]/default/data/search/[table]
    but URLs bust be signed, i.e. linked with
      A('table',_href=URL('data/tables',user_signature=True))
    or with the signed load operator
      LOAD('default','data.load',args='tables',ajax=True,user_signature=True)
    """
    return dict(form=crud())


